import axios from 'axios'
import {BASE_URL} from '@/config/env'
import {login_key} from '@/config/storage-keys'
import Storage from './storage-manage'


axios.defaults.withCredentials = true;
axios.defaults.baseURL = BASE_URL
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
axios.interceptors.request.use((config) => {
    const userInfo = Storage.getLocalStorage(login_key)
    if(userInfo) {
        config.headers['token'] = userInfo.token
    }
    return config
}, error => {
    return Promise.reject(error)
})
axios.interceptors.response.use((response) => {
    return response
})
export default {
    get(url, params,  config = {}) {
        return new Promise((resolve, rejects) => {
            return axios.get(url, {params, config}).then(res => {
                if(res && res.status === 200) {
                    resolve(res.data)
                }
            }).catch(err => {
                rejects(err)
            })
        })
    },
    post(url, params, config = {}) {
        return new Promise((resolve, rejects) => {
            return axios.post(url, params, config).then(res => {
                if(res && res.status === 200) {
                    resolve(res.data)
                }
            }).catch(err => {
                rejects(err)
            })
        })
    }
}